/**
 * \class CDHT
 * \brief This is a basic class to configure DHT-wise toplogy.
 * \version 0.3.2
 *
 * \author Woo-Hyun Kim (woorung@nhncorp.com, woorung@gmail.com)
 * \author http://blog.naver.com/wisereign .
 *
 * \date 2007/10/9
 */
#ifndef CDHTH
#define CDHTH
//-----------------------------------------------------------------
#include <iostream>
#include <vector>
#include <map>
#include <iterator>

#include "common_space.h"
#include "CDHTKey.h"
#include "CDiscoveryProxy.h"

using namespace std;

class CDHT
{
private:
	string resource_type;

	CDHTKey *default_dht_key;
	CDHTKey *dht_key;

	CDiscoveryProxy *discovery;

protected:
	string discovery_ip;
	int discovery_port;

	map<string,CServiceInfo> workspaces;

	void init();
	void fin();

	virtual int connect();

	virtual int _configure(CServiceInfo *key);

	// lookup only local workspaces
	virtual map<string,CServiceInfo> &_llookup(char *key, map<string,CServiceInfo> &found);
	// lookup all the distributed workspaces
	virtual map<string,CServiceInfo>::iterator &_lookup(char *key, map<string,CServiceInfo>::iterator &found);

public:
        CDHT();
        CDHT(char *host, int port);
        virtual ~CDHT();

	virtual int bind(char *host, int port);

	char *get_dip();
	int get_dport();

	char *get_rtype();

	int configure(char *stype);
	int configure(char *tservice, CDHTKey *dhtkey);

	// lookup in local workspaces
	virtual vector<CServiceInfo> &llookup(char *ip, vector<CServiceInfo> &ls) = 0;

	// lookup all in workspaces
	virtual vector<CServiceInfo> &lookup(vector<CServiceInfo> &ls);

	// lookup in distributed workspaces
	virtual CServiceInfo &lookup(char *key);

	// size
	int size();
};
//-----------------------------------------------------------------
#endif
